﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Collections;
using System.Text.RegularExpressions;
using System.Security;
using System.Data;
namespace StreetSmartAdmin
{
    public class Regex_Comm
    {
        public const string REG_EMAIL = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    }

    public partial class index : System.Web.UI.Page
    {
        public static ArrayList maillist;
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (DetailsView0.DefaultMode.Equals("Edit"))
                if (DetailsView0.DataItemCount != 0)
                    DetailsView0.UpdateItem(false);
        }

        protected void ExactSearchButton_Click(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //((Label)Header.FindControl("lblUser")).Text = ((Userinfo)Session["UserInfo"]).fullname; 
            //((Label)Header.FindControl("lblUser")).Text = ((String)Session["UserName"]);
            ADODotNetCRUD crud = new ADODotNetCRUD();
            Userinfo user = crud.GetUser(User.Identity.Name);
            ((Label)Header.FindControl("lblUser")).Text = (user.fullname);
            if (IsPostBack)
            {
                if ((Button7.Visible == false) && (Button6.Visible == true) && (!Label16.Text.Equals("Single")) && SearchDropDownList.SelectedValue.Equals("PromotionCode"))
                    Button9.Visible = true;
                else
                    Button9.Visible = false;
                if (SearchDropDownList.SelectedValue.Equals("PromotionCode"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource13";
                    else
                        GridView1.DataSourceID = "SqlDataSource21";
                }
                else if (SearchDropDownList.SelectedValue.Equals("AccountStatus"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource12";
                    else
                        GridView1.DataSourceID = "SqlDataSource20";
                }
                else if (SearchDropDownList.SelectedValue.Equals("TypeOrg"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource33";
                    else
                        GridView1.DataSourceID = "SqlDataSource34";
                }
                else if (SearchDropDownList.SelectedValue.Equals("StateName"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource11";
                    else
                        GridView1.DataSourceID = "SqlDataSource19";

                }
                else if (SearchDropDownList.SelectedValue.Equals("OperatorSSE"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource42";
                    else
                        GridView1.DataSourceID = "SqlDataSource43";

                }
                else if (SearchDropDownList.SelectedValue.Equals("CurrentProvider"))
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource25";
                    else
                        GridView1.DataSourceID = "SqlDataSource28";

                }
                else if (SearchDropDownList.SelectedValue.Equals("UserID"))
                {
                    if (SearchTextBox.Text == "")
                        GridView1.DataSourceID = "SqlDataSource0";
                    else
                        GridView1.DataSourceID = "SqlDataSource15";
                }
                else if (SearchDropDownList.SelectedValue.Equals("FirstName"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource9";
                        else
                            GridView1.DataSourceID = "SqlDataSource17";
                }
                else if (SearchDropDownList.SelectedValue.Equals("LastName"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource10";
                        else
                            GridView1.DataSourceID = "SqlDataSource18";
                }
                else if (SearchDropDownList.SelectedValue.Equals("Email"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource14";
                        else
                            GridView1.DataSourceID = "SqlDataSource22";
                }
                else if (SearchDropDownList.SelectedValue.Equals("Phone"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource23";
                        else
                            GridView1.DataSourceID = "SqlDataSource26";
                }
                else if (SearchDropDownList.SelectedValue.Equals("CompanyName"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource24";
                        else
                            GridView1.DataSourceID = "SqlDataSource27";
                }
                else if (SearchDropDownList.SelectedValue.Equals("PreferredDay"))
                {
                    if ((Date1.Text == "") || (Date2.Text == ""))
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource31";
                        else
                            GridView1.DataSourceID = "SqlDataSource32";
                }
                else if (SearchDropDownList.SelectedValue.Equals("All"))
                {
                    if (SearchTextBox.Text == "")
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource0";
                        else
                            GridView1.DataSourceID = "SqlDataSource16";
                    else
                        if (Label5.Text.Equals("First"))
                            GridView1.DataSourceID = "SqlDataSource29";
                        else
                            GridView1.DataSourceID = "SqlDataSource30";
                }
                else
                {
                    if (Label5.Text.Equals("First"))
                        GridView1.DataSourceID = "SqlDataSource0";
                    else
                        GridView1.DataSourceID = "SqlDataSource16";
                }

            }
            else
            {
                Label5.Text = "Last";
                Label10.Text = "None";
                Label11.Text = "None";
                Label17.Text = "None";
                Label18.Text = "None";
                Label19.Text = "None";
                Date1.Visible = false;
                Date2.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                GridView1.DataSourceID = "SqlDataSource16";
            }
            //GridView1.AllowPaging = false;
            //GridView1.DataBind();
            //Label12.Text = Convert.ToString(GridView1.Rows.Count);
            //GridView1.AllowPaging = true;
            GridView1.DataBind();
            
        }


        protected void SearchDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label7.Text = "";
            Label8.Text = "";
            if (SearchDropDownList.SelectedValue.Equals("PromotionCode"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                DropDownList2.Visible = true;

            }
            else if (SearchDropDownList.SelectedValue.Equals("TypeOrg"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList6.Visible = false;
                DropDownList5.Visible = true;

            }
            else if (SearchDropDownList.SelectedValue.Equals("AccountStatus"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                DropDownList1.Visible = true;
            }
            else if (SearchDropDownList.SelectedValue.Equals("StateName"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                DropDownList3.Visible = true;
            }
            else if (SearchDropDownList.SelectedValue.Equals("PreferredDay"))
            {
                Label7.Text = "From";
                Label8.Text = "To";
                Date1.Visible = true;
                Date2.Visible = true;
                Date1.Text = "";
                Date2.Text = "";
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
            }
            else if (SearchDropDownList.SelectedValue.Equals("CurrentProvider"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                DropDownList4.Visible = true;
            }
            else if (SearchDropDownList.SelectedValue.Equals("OperatorSSE"))
            {
                Date1.Visible = false;
                Date2.Visible = false;
                SearchTextBox.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = true;

            }
            else
            {
                Date1.Visible = false;
                Date2.Visible = false;
                DropDownList1.Visible = false;
                DropDownList2.Visible = false;
                DropDownList3.Visible = false;
                DropDownList4.Visible = false;
                DropDownList5.Visible = false;
                DropDownList6.Visible = false;
                SearchTextBox.Visible = true;
                SearchTextBox.Text = "";
            }

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Button6.Visible = true;
            Button9.Visible = true;
            Button7.Visible = false;
            Button8.Visible = false;
            pnlSelectEmail.Visible = false;
            Label14.Text = "";
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void CreateNew_Click(object sender, EventArgs e)
        {
            DetailsView0.ChangeMode(DetailsViewMode.Insert);
            DetailsView0.AutoGenerateInsertButton = false;
            MultiView1.ActiveViewIndex = 0;
        }



        protected Boolean haveRecord(string Retailer, string UserID)
        {
            string selectcommand = "select * from " + Retailer + " where UserID=" + UserID + ";";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            System.Data.SqlClient.SqlConnection connection = null;
            try
            {
                connection = new System.Data.SqlClient.SqlConnection(connectionString);
                connection.Open();
                System.Data.SqlClient.SqlCommand command;
                command = connection.CreateCommand();
                command.CommandText = selectcommand;
                int RecordCount = (Int32)command.ExecuteScalar();
                connection.Close();
                if (RecordCount > 0)
                    return true;
            }
            catch (Exception)
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
            return false;
        }

        protected void insertUser(string insertcommand)
        {
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void insertRecord(string Retailer, string UserID)
        {
            string insertcommand = "insert into " + Retailer + " (UserID) values(" + UserID + ");";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void updateNewProvider(string UserID, string NewProvider)
        {
            string updatecommand = "update tbluser set NewProvider='" + NewProvider + "' where UserID=" + UserID + ";";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = updatecommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DetailsView0.ChangeMode(DetailsViewMode.Edit);
            String GridIndex = GridView1.SelectedDataKey.Value.ToString();
            ttb.Text = GridIndex;
            String ProviderCommand = "SELECT NewProvider as NewProvider FROM tblUser where UserID='" + GridIndex + "';";
            String ProviderResult = "";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            System.Data.SqlClient.SqlConnection connection = null;
            try
            {
                connection = new System.Data.SqlClient.SqlConnection(connectionString);
                connection.Open();
                System.Data.SqlClient.SqlCommand command;
                System.Data.SqlClient.SqlDataReader reader;
                command = connection.CreateCommand();
                command.CommandText = ProviderCommand;
                reader = command.ExecuteReader();
                reader.Read();
                ProviderResult = Convert.ToString(reader["NewProvider"]);
                reader.Close();
                connection.Close();
            }
            catch (Exception)
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }


            tbPro.Text = ProviderResult;
            if (ProviderResult.Equals("TRUenergy"))
            {
                ((DetailsView)TRU.FindControl("DetailsView1")).DataBind();
                MultiView1.ActiveViewIndex = 1;
            }
            else if (ProviderResult.Equals("Energy Australia"))
            {
                ((DetailsView)EA.FindControl("DetailsView2")).DataBind();
                MultiView1.ActiveViewIndex = 2;
            }
            else if (ProviderResult.Equals("EA TRU"))
            {
                ((DetailsView)EATRU.FindControl("DetailsView3")).DataBind();
                MultiView1.ActiveViewIndex = 3;
            }
            else if (ProviderResult.Equals("Q Energy"))
            {
                ((DetailsView)Qenergy.FindControl("DetailsView4")).DataBind();
                MultiView1.ActiveViewIndex = 4;
            }
            else if (ProviderResult.Equals("Blue NRG"))
            {
                ((DetailsView)BlueENG.FindControl("DetailsView5")).DataBind();
                //DetailsView5.DataBind();
                MultiView1.ActiveViewIndex = 5;
            }
            else
            {
                DetailsView0.DataBind();
                MultiView1.ActiveViewIndex = 0;
            }
        }

        /*
        public void selTypeOrgChanged(object sender, EventArgs e)
        {
            if (((DropDownList)DetailsView0.FindControl("selTypeOrg")).SelectedValue.Equals("C&I"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    if (!haveRecord("tblCI", ((Label)DetailsView0.FindControl("lbluserID")).Text))
                    {
                        insertRecord("tblCI", ((Label)DetailsView0.FindControl("lbluserID")).Text);
                    }
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, "C&I");
                    DetailsView5.DataBind();
                    MultiView1.ActiveViewIndex = 5;
                }
                else
                {
                    ((TextBox)DetailsView0.FindControl("NewProvider")).Text = "C&I";
                }
            }
            else
                MultiView1.ActiveViewIndex = 0;
        }
        */
        public void selProviderChanged(object sender, EventArgs e)
        {

            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals(""))
            {
                MultiView1.ActiveViewIndex = 0;
            }

            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals("TRUenergy"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, ((TextBox)DetailsView0.FindControl("NewProvider")).Text);
                    ((DetailsView)TRU.FindControl("DetailsView1")).DataBind();
                    MultiView1.ActiveViewIndex = 1;
                }

            }
            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals("Energy Australia"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    if (!haveRecord("tblEA", ((Label)DetailsView0.FindControl("lbluserID")).Text))
                    {
                        insertRecord("tblEA", ((Label)DetailsView0.FindControl("lbluserID")).Text);
                    }
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, ((TextBox)DetailsView0.FindControl("NewProvider")).Text);
                    ((DetailsView)EA.FindControl("DetailsView2")).DataBind();
                    MultiView1.ActiveViewIndex = 2;
                }
            }


            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals("EA TRU"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    if (!haveRecord("EA_TRU", ((Label)DetailsView0.FindControl("lbluserID")).Text))
                    {
                        insertRecord("EA_TRU", ((Label)DetailsView0.FindControl("lbluserID")).Text);
                    }
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, ((TextBox)DetailsView0.FindControl("NewProvider")).Text);
                    ((DetailsView)EATRU.FindControl("DetailsView3")).DataBind();
                    MultiView1.ActiveViewIndex = 3;
                }
            }

            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals("Q Energy"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    if (!haveRecord("QEnergy", ((Label)DetailsView0.FindControl("lbluserID")).Text))
                    {
                        insertRecord("QEnergy", ((Label)DetailsView0.FindControl("lbluserID")).Text);
                    }
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, ((TextBox)DetailsView0.FindControl("NewProvider")).Text);
                    ((DetailsView)Qenergy.FindControl("DetailsView4")).DataBind();
                    MultiView1.ActiveViewIndex = 4;
                }
            }

            if (((DropDownList)DetailsView0.FindControl("selNewProvider")).SelectedValue.Equals("Blue NRG"))
            {
                if (!((Label)DetailsView0.FindControl("lbluserID")).Text.Equals(""))
                {
                    if (!haveRecord("Blue_NRG", ((Label)DetailsView0.FindControl("lbluserID")).Text))
                    {
                        insertRecord("Blue_NRG", ((Label)DetailsView0.FindControl("lbluserID")).Text);
                    }
                    updateNewProvider(((Label)DetailsView0.FindControl("lbluserID")).Text, ((TextBox)DetailsView0.FindControl("NewProvider")).Text);
                    ((DetailsView)BlueENG.FindControl("DetailsView5")).DataBind();
                    MultiView1.ActiveViewIndex = 5;
                }
            }
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            DetailsView0.ChangeMode(DetailsViewMode.Edit);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (SearchDropDownList.SelectedValue.Equals("PromotionCode"))
            {
                GridView1.DataSourceID = "SqlDataSource21";
            }
            else if (SearchDropDownList.SelectedValue.Equals("OperatorSSE"))
            {
                GridView1.DataSourceID = "SqlDataSource43";
            }
            else if (SearchDropDownList.SelectedValue.Equals("AccountStatus"))
            {
                GridView1.DataSourceID = "SqlDataSource20";
            }
            else if (SearchDropDownList.SelectedValue.Equals("TypeOrg"))
            {
                GridView1.DataSourceID = "SqlDataSource34";
            }
            else if (SearchDropDownList.SelectedValue.Equals("StateName"))
            {
                GridView1.DataSourceID = "SqlDataSource19";
            }
            else if (SearchDropDownList.SelectedValue.Equals("CurrentProvider"))
            {
                GridView1.DataSourceID = "SqlDataSource28";
            }
            else if (SearchDropDownList.SelectedValue.Equals("UserID"))
            {
                if (SearchTextBox.Text == "")
                {
                    GridView1.DataSourceID = "SqlDataSource16";
                }
                else
                {
                    GridView1.DataSourceID = "SqlDataSource15";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("FirstName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource17";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("LastName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource18";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("Email"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource22";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("Phone"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource26";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("CompanyName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource27";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("PreferredDay"))
            {
                if ((Date1.Text == "") || (Date2.Text == ""))
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource32";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("All"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource16";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource30";
                }
            }
            else
            {
                GridView1.DataSourceID = "SqlDataSource16";
            }

            Label5.Text = "Last";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            if (SearchDropDownList.SelectedValue.Equals("PromotionCode"))
            {
                GridView1.DataSourceID = "SqlDataSource13";
            }
            else if (SearchDropDownList.SelectedValue.Equals("OperatorSSE"))
            {
                GridView1.DataSourceID = "SqlDataSource42";
            }
            else if (SearchDropDownList.SelectedValue.Equals("AccountStatus"))
            {
                GridView1.DataSourceID = "SqlDataSource12";
            }
            else if (SearchDropDownList.SelectedValue.Equals("TypeOrg"))
            {
                GridView1.DataSourceID = "SqlDataSource33";
            }
            else if (SearchDropDownList.SelectedValue.Equals("StateName"))
            {
                GridView1.DataSourceID = "SqlDataSource11";
            }
            else if (SearchDropDownList.SelectedValue.Equals("CurrentProvider"))
            {
                GridView1.DataSourceID = "SqlDataSource25";
            }
            else if (SearchDropDownList.SelectedValue.Equals("UserID"))
            {
                if (SearchTextBox.Text == "")
                {
                    GridView1.DataSourceID = "SqlDataSource0";
                }
                else
                {
                    GridView1.DataSourceID = "SqlDataSource15";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("FirstName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource9";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("LastName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource10";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("Email"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource14";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("Phone"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource23";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("CompanyName"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource24";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("PreferredDay"))
            {
                if ((Date1.Text == "") || (Date2.Text == ""))
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource31";
                }
            }
            else if (SearchDropDownList.SelectedValue.Equals("All"))
            {
                if (SearchTextBox.Text == "")
                    GridView1.DataSourceID = "SqlDataSource0";
                else
                {
                    GridView1.DataSourceID = "SqlDataSource29";
                }
            }
            else
            {
                GridView1.DataSourceID = "SqlDataSource0";
            }

            Label5.Text = "First";
        }

        protected void PageSizeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridView1.PageSize = Convert.ToInt32(PageSizeList.SelectedValue);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment;filename= " + Server.UrlEncode("Export.xls"));
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("EN", true);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            GridView1.AllowPaging = true;
        }

        override public void VerifyRenderingInServerForm(Control control)
        { }

        protected void Button4_Click(object sender, EventArgs e)
        {

        }

        protected void Date1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Date2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (Label10.Text.Equals("None") || Label11.Text.Equals("None") || Label17.Text.Equals("None") || Label18.Text.Equals("None") || Label19.Text.Equals("None"))
            {
                Label10.Text = "Converted";
                Label11.Text = "Not Interested";
                Label17.Text = "Solar";
                Label18.Text = "C&I";
                Label19.Text = "Cannot Quote";

            }
            else
            {
                Label10.Text = "None";
                Label11.Text = "None";
                Label17.Text = "None";
                Label18.Text = "None";
                Label19.Text = "None";
            }
        }

        public void SendMail(String receiver, String subject, String body)
        {
            MailMessage msg = new MailMessage();
            msg.To.Add(receiver);
            msg.From = new MailAddress("Lisa@StreetSmartEnergy.com.au", "Street Smart Energy", System.Text.Encoding.UTF8);
            msg.Subject = subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = body;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Host = "localhost";
            //object userState = msg; 
            //try
            //{    
            //client.SendAsync(msg, userState); 
            client.Send(msg);
            //Label14.Text="Success!"; 
            //} 
            //catch (System.Net.Mail.SmtpException )
            //{ 
            //Label14.Text=("Error When Sending Email!"); 
            //}
        }

        public void SendBulkMail(String subject, String body)
        {
            MailMessage msg = new MailMessage();
            for (int i = 0; i < maillist.Count; i++)
            {
                msg.To.Add((String)maillist[i]);
            }
            msg.From = new MailAddress("Lisa@StreetSmartEnergy.com.au", "Street Smart Energy", System.Text.Encoding.UTF8);
            msg.Subject = subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = body;
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Host = "localhost";
            //object userState = msg; 
            //try
            //{
            //client.SendAsync(msg, userState); 
            client.Send(msg);
            //Label14.Text = "Success!";
            //}
            //catch (System.Net.Mail.SmtpException)
            //{
            //    Label14.Text = ("Error!");
            //}
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if ((TextBox)DetailsView0.FindControl("Email") != null)
            {
                Button6.Visible = false;
                Button7.Visible = true;
                Button8.Visible = true;
                pnlSelectEmail.Visible = true;
                Label14.Text = ((TextBox)DetailsView0.FindControl("Email")).Text;
                Label16.Text = "Single";
            }
        }

        protected void Button7_Click(object sender, EventArgs e)
        {

            String subject = lblTemplateSubject.Text;
            String body = lblTemplateBody.Text;
            if (Label16.Text.Equals("Single"))
            {
                String receiver = ((TextBox)DetailsView0.FindControl("Email")).Text;
                SendMail(receiver, subject, body);
                Label14.Text = "Success!";
            }
            else if (Label16.Text.Equals("Bulk") && (maillist.Count > 0))
            {
                for (int i = 0; i < maillist.Count; i++)
                {
                    SendMail((String)maillist[i], subject, body);
                }
                Label14.Text = "Success!";
                //SendBulkMail(subject, body);
            }
            Button6.Visible = true;
            Button7.Visible = false;
            Button8.Visible = false;
            Button9.Visible = true;
            pnlSelectEmail.Visible = false;
            Label16.Text = "";
        }

        protected void ddlEmail_SelectedIndexChanged(object sender, EventArgs e)
        {
            String EmailTemplateName;
            String EmailTemplateSubject;
            String EmailTemplateBody;
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            SqlConnection connection = null;
            try
            {
                connection = new SqlConnection(connectionString);
                connection.Open();
                SqlCommand command;
                SqlDataReader reader;
                command = connection.CreateCommand();
                command.CommandText = "select * from tblEmailTemplate where EmailTemplateID=" + ddlET.SelectedValue;
                reader = command.ExecuteReader();
                reader.Read();
                EmailTemplateName = Convert.ToString(reader["EmailTemplateName"]);
                EmailTemplateSubject = Convert.ToString(reader["EmailTemplateSubject"]);
                EmailTemplateBody = Convert.ToString(reader["EmailTemplateBody"]);
                reader.Close();
                connection.Close();
                lblTemplateName.Text = EmailTemplateName;
                lblTemplateSubject.Text = EmailTemplateSubject;
                lblTemplateBody.Text = EmailTemplateBody;
            }
            catch (Exception)
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            Button6.Visible = true;
            Button7.Visible = false;
            Button8.Visible = false;
            pnlSelectEmail.Visible = false;
            Label14.Text = "";
            Label16.Text = "";
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            String address = "";
            maillist = new ArrayList();
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;
            SqlConnection connection = null;
            connection = new SqlConnection(connectionString);
            connection.Open();
            SqlCommand command;
            SqlDataReader reader;
            command = connection.CreateCommand();
            command.CommandText = "select UserID, Email from tblUser where PromotionCode='" + DropDownList2.SelectedValue + "' order by UserID;";
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                if (IsValidEmail(Convert.ToString(reader["Email"])))
                    maillist.Add((Convert.ToString(reader["Email"])).Trim());
            }
            reader.Close();
            connection.Close();

            for (int i = 0; i < maillist.Count; i++)
            {
                address = address + maillist[i] + ";";
            }
            Label14.Text = address;
            Button6.Visible = false;
            Button7.Visible = true;
            Button8.Visible = true;
            Button9.Visible = false;
            pnlSelectEmail.Visible = true;
            Label16.Text = "Bulk";
        }



        public static bool IsValidEmail(string email)
        {
            return Regex.IsMatch(email, Regex_Comm.REG_EMAIL);
        }

        private void MessageBox(string message)
        {
            if (!string.IsNullOrEmpty(message))
            {
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alt1", "alert('" + message + "');", true);
            }
        }


        private string UserCap(string username)
        {
            string str1 = username.Substring(0, 1);
            string str2 = username.Substring(1, username.Length - 1);
            return str1.ToUpper() + str2;
        }
    }
}